<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title>工程师绑定</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
</head>
<body>
<h2 style="color:#666;text-align:center;">维修工程师绑定</h2>

<div class="weui-cells weui-cells_form">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="rname"
                   placeholder="请输入姓名"/>
        </div>
        <div class="weui-cell__ft">
            <i class="weui-icon-warn"></i>
        </div>
    </div>
    <div class="weui-cell weui-cell_vcode">
        <div class="weui-cell__hd">
            <label class="weui-label">手机号</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="tel"
                   type="number" placeholder="请输入手机号"/>
        </div>
        <div class="weui-cell__ft">
            <button class="weui-vcode-btn">获取验证码</button>
        </div>
    </div>


    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">验证码</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="number" id="yzm"
                   placeholder="请输入验证码"/>
        </div>
    </div>

    <div class="weui-cell">
    <div class="weui-cell__bd">
        <div class="weui-uploader">
            <div class="weui-uploader__hd">
                <p class="weui-uploader__title">身份证正面</p>
                <!--<div class="weui-uploader__info">0/5</div>-->
            </div>
            <div class="weui-uploader__bd">
                <ul class="weui-uploader__files" id="uploaderFiles">
                    <li id="id_zheng"
                        style="display:none"
                        class="weui-uploader__file"
                    >

                    </li>

                </ul>

                <div class="weui-uploader__input-box" onclick="upload(1)">

                </div>
            </div>
        </div>
    </div>
</div>

    <div class="weui-cell">
        <div class="weui-cell__bd">
            <div class="weui-uploader">
                <div class="weui-uploader__hd">
                    <p class="weui-uploader__title">身份证反面</p>
                    <!--<div class="weui-uploader__info">0/5</div>-->
                </div>
                <div class="weui-uploader__bd">
                    <ul class="weui-uploader__files">
                        <li id="id_fan"
                            class="weui-uploader__file"
                            style="display:none;"
                        >

                        </li>

                    </ul>

                    <div class="weui-uploader__input-box" onclick="upload(2)">

                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="weui-cell">
        <div class="weui-cell__bd">
            <div class="weui-uploader">
                <div class="weui-uploader__hd">
                    <p class="weui-uploader__title">头像</p>
                    <!--<div class="weui-uploader__info">0/5</div>-->
                </div>
                <div class="weui-uploader__bd">
                    <ul class="weui-uploader__files">
                        <li id="person_pic"
                            class="weui-uploader__file"
                            style="display:none;"
                        >

                        </li>

                    </ul>

                    <div class="weui-uploader__input-box" onclick="upload(3)">

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<a href="javascript:;" onclick="bangding()" style="width:90%;margin-top:20px;" class="weui-btn weui-btn_primary">绑定</a>

<!--<a href="javascript:;" onclick="location='equipment_add.html'"-->
   <!--style="width:90%;margin-top:20px;margin-bottom:30px;" class="weui-btn weui-btn_primary">设备录入</a>-->

<!-- body 最后 -->
<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>

<!-- 如果使用了某些拓展插件还需要额外的JS -->
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/swiper.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/city-picker.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script src="js/jssdk.js"></script>
<script>

    jssdk(function(){}, ['chooseImage','getLocalImgData','uploadImage']);
    var id_zheng_pic,id_fan_pic,person_pic_pic;
    function upload(num){
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表，
                    $.showLoading("请稍后...")
                    wx.getLocalImgData({
                        localId: localIds[0], // 图片的localID
                        success: function (res) {
                            var localData = res.localData;
                            $.post("/ossupload",{
                                    base64:localData
                                },function(data){
                                    //data ==>pName
                                    if(num==1){
                                        id_zheng.style.display = "";
                                        //alert("http://java59.hfbdqn.cn/ossdownload?pName="+pName);
                                        id_zheng_pic = "http://java59.hfbdqn.cn/ossdownload?pName=" +data;
                                        id_zheng.style.background = "url("+id_zheng_pic+") no-repeat";
                                        id_zheng.style['background-size'] = "contain";
                                    }else if(num==2){
                                        id_fan_pic = "http://java59.hfbdqn.cn/ossdownload?pName=" +data;
                                        id_fan.style.display = "";
                                        id_fan.style.background = "url(http://java59.hfbdqn.cn/ossdownload?pName="
                                            +data+") no-repeat";
                                        id_fan.style['background-size'] = "contain";
                                    }else if(num==3){
                                        person_pic_pic = "http://java59.hfbdqn.cn/ossdownload?pName=" +data;
                                        person_pic.style.display = "";
                                        person_pic.style.background = "url(http://java59.hfbdqn.cn/ossdownload?pName="
                                            +data+") no-repeat";
                                        person_pic.style['background-size'] = "contain";
                                    }

                                    $.hideLoading();
                                })
                        }
                    });
                }
            });

    }

    $.showLoading("正在加载...");
    var openid = "";
    $(function () {
        var url = document.URL;
       // alert(url);
        var start = url.indexOf("code=");
        var end = url.indexOf("&");
        var code = url.substring(start + 5, end);

        $.get("/find_openid", {code: code}, function (data) {
            //   alert(data);
            openid = data;

            localStorage.openid = openid;

            $.get("/find_repairman_byopenid", {openid: openid}, function (data) {
                if (data) {
                    localStorage.repairMan = JSON.stringify(data);
                    location = 'repair_main.html';
                }else{
                    $.hideLoading();
                }
            });

        })
    });
</script>

<script>


    function check() {
        if (rname.value.trim().length == 0) {
            $("#rname").parents(".weui-cell").addClass("weui-cell_warn");
            return false;
        }

        if (tel.value.trim().length == 0) {
            $.toast("手机号必填哦，亲", "text");
            $("#tel").parents(".weui-cell").addClass("weui-cell_warn");
            return false;
        }

        var reg = /1[3-9]\d{9}/;
        if (!reg.test($("#tel").val())) {
            $.toast("手机号格式不对哦，亲", "text");
            $("#tel").parents(".weui-cell").addClass("weui-cell_warn");
            return false;
        }

        return true;
    }

    $(function () {
        var codeBtnOK = true;
        $(".weui-vcode-btn").click(function () {

            if (codeBtnOK) {

                $.get("sendsms", {tel: tel.value}, function (data) {
                    if (data) {
                        $.toast("短信已经发送！");
                    }

                });

                var t = 5;
                codeBtnOK = false;
                $(this).text(t + "秒后重新发送");

                var timer = setInterval(function () {
                        $(".weui-vcode-btn").text(--t + "秒后重新发送");
                        if (t == 1) {
                            clearInterval(timer);
                            codeBtnOK = true;
                            $(".weui-vcode-btn").text("获取验证码");
                        }
                    },
                    1000)
            }


        });
        $("#rname,#tel").focus(function () {
            $(this).parents(".weui-cell").removeClass("weui-cell_warn");
        });

        $("#rname,#tel").blur(function () {
            if ($(this).val().trim().length == 0) {
                $(this).parents(".weui-cell").addClass("weui-cell_warn");
                return;
            }

            var $p = $(this);
            $.get("tel_isrepeat", {tel: tel.value}, function (data) {
                if (!data) {
                    $.toast("这个号码已经绑定！", "cancel")
                    $p.parents(".weui-cell").addClass("weui-cell_warn");
                    return;
                }
            })
        });


    });

    function bangding() {
        if (!check()) {
            return;
        }

        $.ajax({
            type: 'post',
            url: 'repair_man_bind',
            data: JSON.stringify({
                name: rname.value,
                tel: tel.value,
                openid: openid,
                yzm: yzm.value,
                id_zheng:id_zheng_pic,
                id_fan:id_fan_pic,
                person_pic:person_pic_pic
            }),
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                if (data) {
                    $.toast("谢谢您的绑定！")
                    localStorage.repairMan = JSON.stringify(data);
                    location = 'repair_main.html'

                } else {
                    $.toast("绑定失败，请您重试！", "cancel")
                }

            }
        });
    }
</script>
</body>
</html>